package com.file.manage.infrastructure.config;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.file.manage.domain.data.UserInfo;
import com.file.manage.domain.enums.UserPermission;
import com.file.manage.domain.enums.UserRole;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限验证接口
 *
 * @author Hollis
 */
@Component
public class StpInterfaceImpl implements StpInterface {
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        UserInfo userInfo = (UserInfo) StpUtil.getSessionByLoginId(loginId).get((String) loginId);
        List<String> permissionList = new ArrayList<>();
        switch (userInfo.getRole()){
            case ADMIN -> {
                for (UserPermission value : UserPermission.values()) {
                    permissionList.add(value.name());
                }
                return permissionList;
            }
            case USER -> {
                permissionList.add(UserPermission.VIEW.name());
                permissionList.add(UserPermission.UPLOAD.name());
                permissionList.add(UserPermission.EDIT.name());
                permissionList.add(UserPermission.MOVE.name());
            }
            default -> throw new IllegalStateException("Unexpected value: " + userInfo.getRole());
        }
      return permissionList;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        UserInfo userInfo = (UserInfo) StpUtil.getSessionByLoginId(loginId).get((String) loginId);
        if (userInfo.getRole() == UserRole.ADMIN) {
            return List.of(UserRole.ADMIN.name());
        }
        return List.of(UserRole.USER.name());
    }
}
